<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-input
          v-model="dataForm.username"
          placeholder="请输入名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="dataForm.mobile"
          placeholder="请输入手机号"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()" v-if="isAuth('user:user:add')">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
       <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="头像"
      >
        <template slot-scope="scope">
          <img :src="'https://yunchenbucket.oss-cn-qingdao.aliyuncs.com/' + scope.row.tx" alt="" width="60px" height="60px" style="border-radius: 50%;">
        </template>
      </el-table-column>
      <el-table-column
        prop="username"
        header-align="center"
        align="center"
        label="名字"
      >
      </el-table-column>
      <el-table-column
        prop="mobile"
        header-align="center"
        align="center"
        label="手机号"
      >
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="注册时间"
      >
      </el-table-column>
      <el-table-column
        prop="ifJy"
        header-align="center"
        align="center"
        label="状态"
      >
      <template slot-scope="{row}">
          <el-tag :type="row.authentication === 0 ? '' : 'success'">
            {{ row.authentication | typeFilter }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <!-- <el-button
            type="text"
            size="small"
            @click="addOrAddsHandle(scope.row.id)"
            >发货</el-button
          > -->
          <!-- <el-button
            type="text"
            size="small"
            v-if="scope.row.ifJy == 1 && isAuth('user:user:enable')"
            @click="sjBnt(scope.row.userId,0)"
            >启用</el-button
          > -->
          <el-button
            type="text"
            size="small"
            v-if="scope.row.ifJy == 0  && isAuth('user:user:forbidden')"
            @click="openDialog(scope.row.userId)"
            >详情</el-button
          >
          <el-button
            type="text"
            size="small"
            v-if="scope.row.authentication == 1  && isAuth('user:user:forbidden')"
            @click="sjBnt(scope.row.userId)"
            >认证</el-button
          >
          <!-- <el-button
            type="text" 
            size="small"
            v-if="isAuth('user:user:set')"
            @click="addOrUpdateBnt(scope.row.userId)"
            >修改</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
    <!-- 发货弹窗 -->
    <add-or-add
      v-if="addOrAddVisible"
      ref="addOrAdds"
      @refreshDataList="getDataList"
    ></add-or-add>
    <el-dialog
    title="用户信息"
    :visible.sync="dialogVisible"
    width="800px"
    :close-on-click-modal="false">
    <el-form label-width="120px">
      <!-- 左侧信息列 -->
      <div class="form-column">
        <el-form-item label="登录账号" prop="username">
          <el-input v-model="formData.username" disabled></el-input>
        </el-form-item>
        
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="formData.mobile" :disabled="!isEdit"></el-input>
        </el-form-item>

        <el-form-item label="用户类型">
          <el-tag :type="typeStyleMap[formData.type]">
            {{ typeTextMap[formData.type] || '未知类型' }}
          </el-tag>
        </el-form-item>

        <el-form-item label="创建时间">
          {{ formData.createTime }}
        </el-form-item>
      </div>

      <!-- 右侧信息列 -->
      <div class="form-column">
        <el-form-item label="会员到期">
          {{ formData.memberEndTime || '--' }}
        </el-form-item>

        <el-form-item label="认证状态">
          <el-tag :type="formData.authentication ? 'success' : 'danger'">
            {{ formData.authentication ? '未认证' : '已认证' }}
          </el-tag>
        </el-form-item>

        <el-form-item label="社区名称">
          {{ formData.communityName }}
        </el-form-item>

        <el-form-item label="邀请码">
          <el-input v-model="formData.invitationCode" disabled></el-input>‌
        </el-form-item>
      </div>
    </el-form>

    <div slot="footer">
      <el-button @click="dialogVisible = false">关闭</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </div>
  </el-dialog>
  </div>
</template>

<script>
import AddOrUpdate from "./user-add-or-update";
import AddOrAdd from "./user-add-or-add";
export default {
  filters: {
    typeFilter(type) {
      const typeMap = { 0: '认证', 1: '未认证' }
      return typeMap[type] || '未知类型'
    }
  },
  data() {
    return {
      dialogVisible: false,
      isEdit: false,
      typeTextMap: {1:'普通会员',2:'超级会员',3:'普通用户'},
      typeStyleMap: {1:'warning',2:'danger',3:'info'},
      formData: {
        username: '',
        mobile: '',
        type: 1,
        createTime: '',
        memberEndTime: '',
        authentication: 0,
        invitationCode: 'INV20250225',
        communityName: '智能社区'
      },
      dataForm: {
        username: "",
        mobile : ""
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      addOrAddVisible: false
    };
  },
  components: {
    AddOrUpdate,
    AddOrAdd
  },
  activated() {
    this.getDataList();
  },
  methods: {
    openDialog(id) {
      this.dialogVisible = true
      this.formData.userId = id 
        this.$http({
            url: this.$http.adornUrl(
              `yshbms/user/info/${this.formData.userId}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.formData = data.userEntity;
              this.formData.id = id 
              console.log(this.dataForm);
            }
          });
    },
    // 时间格式化‌:ml-citation{ref="1,2" data="citationList"}
    formatTime(time) {
      // return time ? dayjs(time).format('YYYY-MM-DD HH:mm') : '--'
    },
    // 用户类型映射‌:ml-citation{ref="1,4" data="citationList"}
    getUserTypeText(type) {
      const map = {1: '普通会员', 2: '超级会员', 3: '普通用户'}
      return map[type] || '未知类型'
    },
    getUserTypeStyle(type) {
      const styleMap = {1: 'warning', 2: 'danger', 3: 'info'}
      return styleMap[type] || ''
    },
    handleSubmit() {
      this.$http({
              url: this.$http.adornUrl(`yshbms/user/update`),
              method: "post",
              params: this.$http.adornParams(this.detailData),
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.dialogVisible = false;
                    this.getDataList();
                  },
                });
              } else {
                this.$message.error(data.msg);
              }
            });
      this.dialogVisible = false
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("yh/user/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          username: this.dataForm.username,
          mobile:  this.dataForm.mobile
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page;
          this.totalPage = data.total;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 上架
    sjBnt(id,status) {
      this.$confirm('此操作将认证用户, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
          url: this.$http.adornUrl("yshbms/user/update"),
          method: "post",
          params: this.$http.adornParams({
            userId: id,
            authentication: 0,
          }),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.getDataList()
            this.$message({
            type: 'success',
            message: '操作成功!'
          });
          } else {
            this.$message({
            type: 'info',
            message: '操作失败'
          });  
          }
        });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });          
        });
    },
    // 下架
    xjBnt(id,status) {
      this.$confirm('此操作将用户禁用, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
          url: this.$http.adornUrl("yh/user/update"),
          method: "post",
          params: this.$http.adornParams({
            userId: id,
            ifJy: status,
          }),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.getDataList()
            this.$message({
            type: 'success',
            message: '操作成功!'
          });
          } else {
            this.$message({
            type: 'info',
            message: '操作失败'
          });  
          }
        });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });          
        });
    },
    mrtxBnt(id) {
      this.$confirm('此操作将用户禁用, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
              url: this.$http.adornUrl(`yh/user/updateTx`),
              method: "post",
              params: this.$http.adornParams({
              userId: id
              }),
            }).then(({ data }) => {
          if (data && data.code === 0) {
            this.getDataList()
            this.$message({
            type: 'success',
            message: '操作成功!'
          });
          } else {
            this.$message({
            type: 'info',
            message: '操作失败'
          });  
          }
        });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });          
        });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 修改
    addOrUpdateBnt(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 详情 
    addOrAddsHandle(id) {
      this.addOrAddVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrAdds.init(id);
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.id;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("sys/creativegoods/delete"),
            method: "post",
            data: this.$http.adornData(ids, false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>
